'''
Created on Mar 10, 2010

@author: sternron
'''
from cliques.potentialBasedSearch import potentialBasedSearch
import random
from cliques.closest.closestCliqueSearch import ClosestCliqueSearch
from cliques.closest.ongoingMemberSearch import OngoingMemberSearch

class OngoingCliqueSearch(OngoingMemberSearch):
    '''
    An anytime k-clique search algorithm. Continuously outputs clique members,
    and allows listeners to by notified when cliques are found.
    '''
    
    def __init__(self):
        '''
        Constructor
        '''
        OngoingMemberSearch.__init__(self)
                
    def notify_clique_found(self,a_clique):
        ''' An event that is raised when a new k-clique has been found '''
        self.fullPotential = a_clique
        self.notify_member_listeners(a_clique)
        self.found_members.update(a_clique) # Append a_clique members to found_clique_members

    def calculate_node_value(self,node):
        ''' Return the cost function for this node '''
        max_core = 0
        max_kPc = None
        if self.generated_to_kPc.has_key(node):
            for kPc in self.generated_to_kPc[node]:
                if len(kPc.core)>max_core:
                    max_core = len(kPc.core)
                    max_kPc=kPc

        max_core = max(self.k-1,max_core) # Cliques larger than k recieve same grade as k-cliques
        return self.g[node]+self.k-1-max_core
    
    def __str__(self):
        return "OngoingCliqueSearch"
